package com.cms.config;


import com.cms.template.directive.*;
import com.cms.template.method.AbbreviateMethod;
import com.cms.template.method.DateMethod;
import com.cms.template.method.IntersectionMethod;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;

@Configuration
public class FreemarkerConfig {

    @Autowired
    private Environment env;

    @Autowired
    private freemarker.template.Configuration configuration;

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private CategoryChildrenListDirective categoryChildrenListDirective;

    @Autowired
    private CategoryDirective categoryDirective;

    @Autowired
    private CategoryParentListDirective categoryParentListDirective;

    @Autowired
    private CategoryRootListDirective categoryRootListDirective;

    @Autowired
    private ContentDirective contentDirective;

    @Autowired
    private ContentListDirective contentListDirective;

    @Autowired
    private ContentPageDirective contentPageDirective;

    @Autowired
    private FriendLinkListDirective friendLinkListDirective;

    @Autowired
    private MenuRootListDirective menuRootListDirective;

    @Autowired
    private NavChildrenListDirective navChildrenListDirective;

    @Autowired
    private NavDirective navDirective;

    @Autowired
    private NavParentListDirective navParentListDirective;

    @Autowired
    private NavRootListDirective navRootListDirective;

    @Autowired
    private PaginationDirective paginationDirective;

    @Autowired
    private SiteListDirective siteListDirective;

    @Autowired
    private SlideListDirective slideListDirective;

    @Autowired
    private SqlListDirective sqlListDirective;

    @Autowired
    private SqlPageDirective sqlPageDirective;

    @Autowired
    private TagListDirective tagListDirective;

    @Autowired
    private AbbreviateMethod abbreviateMethod;

    @Autowired
    private IntersectionMethod intersectionMethod;

    @Autowired
    private DateMethod dateMethod;


    @PostConstruct
    public void setSharedVariable() throws TemplateModelException{
        configuration.setSharedVariable("base", servletContext.getContextPath());
        configuration.setSharedVariable("category_children_list", categoryChildrenListDirective);
        configuration.setSharedVariable("category", categoryDirective);
        configuration.setSharedVariable("category_parent_list", categoryParentListDirective);
        configuration.setSharedVariable("category_root_list", categoryRootListDirective);
        configuration.setSharedVariable("content", contentDirective);
        configuration.setSharedVariable("content_list", contentListDirective);
        configuration.setSharedVariable("content_page", contentPageDirective);
        configuration.setSharedVariable("friend_link_list", friendLinkListDirective);
        configuration.setSharedVariable("menu_root_list", menuRootListDirective);
        configuration.setSharedVariable("nav_children_list", navChildrenListDirective);
        configuration.setSharedVariable("nav", navDirective);
        configuration.setSharedVariable("nav_parent_list", navParentListDirective);
        configuration.setSharedVariable("nav_root_list", navRootListDirective);
        configuration.setSharedVariable("pagination", paginationDirective);
        configuration.setSharedVariable("site_list", siteListDirective);
        configuration.setSharedVariable("slide_list", slideListDirective);
        configuration.setSharedVariable("sql_list", sqlListDirective);
        configuration.setSharedVariable("sql_page", sqlPageDirective);
        configuration.setSharedVariable("tag_list", tagListDirective);
        configuration.setSharedVariable("abbreviate", abbreviateMethod);
        configuration.setSharedVariable("intersection", intersectionMethod);
        configuration.setSharedVariable("date", dateMethod);
    }
}
